package com.zhny.project.exec.algorithm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhny.framework.config.RuoYiConfig;
import com.zhny.project.count.algorithm.domain.Algorithm;
import com.zhny.project.count.algorithm.service.IAlgorithmService;
import com.zhny.project.count.ification.domain.Ification;
import com.zhny.project.count.ification.service.IIficationService;
import com.zhny.project.shell.IShellService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.project.exec.algorithm.domain.ExecAlgorithm;
import com.zhny.project.exec.algorithm.service.IExecAlgorithmService;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.web.page.TableDataInfo;

/**
 * 算法执行程序Controller
 * 
 * @author lxh
 * @date 2019-10-17
 */
@Controller
@RequestMapping("/exec/algorithm")
public class ExecAlgorithmController extends BaseController
{
    private String prefix = "exec/algorithm";

    @Autowired
    private IExecAlgorithmService execAlgorithmService;
    @Autowired
    private IIficationService ificationService;
    @Autowired
    private IAlgorithmService algorithmService;
    @Autowired
    private IShellService shellService;

    @RequiresPermissions("exec:algorithm:view")
    @GetMapping()
    public String algorithm(Model model)
    {
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        model.addAttribute("ification",ifications);
        return prefix + "/algorithm";
    }

    /**
     * 查询算法执行程序列表
     */
    @RequiresPermissions("exec:algorithm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExecAlgorithm execAlgorithm)
    {
        startPage();
        List<ExecAlgorithm> list = execAlgorithmService.selectExecAlgorithmList(execAlgorithm);
        return getDataTable(list);
    }

    /**
     * 导出算法执行程序列表
     */
    @RequiresPermissions("exec:algorithm:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExecAlgorithm execAlgorithm)
    {
        List<ExecAlgorithm> list = execAlgorithmService.selectExecAlgorithmList(execAlgorithm);
        ExcelUtil<ExecAlgorithm> util = new ExcelUtil<ExecAlgorithm>(ExecAlgorithm.class);
        return util.exportExcel(list, "algorithm");
    }

    /**
     * 新增算法执行程序
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        model.addAttribute("ification",ifications);
        return prefix + "/add";
    }

    /**
     * 新增保存算法执行程序
     */
    @RequiresPermissions("exec:algorithm:add")
    @Log(title = "算法执行程序", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExecAlgorithm execAlgorithm)
    {
        return toAjax(execAlgorithmService.insertExecAlgorithm(execAlgorithm));
    }

    /**
     * 修改算法执行程序
     */
    @GetMapping("/edit/{exceKey}")
    public String edit(@PathVariable("exceKey") String exceKey, ModelMap mmap)
    {
        ExecAlgorithm execAlgorithm = execAlgorithmService.selectExecAlgorithmById(exceKey);
        mmap.put("execAlgorithm", execAlgorithm);
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        mmap.put("ification",ifications);
        Algorithm algorithm = new Algorithm();
        algorithm.setIficationKey(execAlgorithm.getIficationKey());
        List<Algorithm> algorithms = algorithmService.selectAlgorithmList(algorithm);
        mmap.put("algorithms",algorithms);

        return prefix + "/edit";
    }

    /**
     * 修改保存算法执行程序
     */
    @RequiresPermissions("exec:algorithm:edit")
    @Log(title = "算法执行程序", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExecAlgorithm execAlgorithm)
    {
        return toAjax(execAlgorithmService.updateExecAlgorithm(execAlgorithm));
    }

    /**
     * 删除算法执行程序
     */
    @RequiresPermissions("exec:algorithm:remove")
    @Log(title = "算法执行程序", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(execAlgorithmService.deleteExecAlgorithmByIds(ids));
    }

    /**
     * 详细
     */
    @GetMapping("/detail/{exceKey}")
    public String detail(@PathVariable("exceKey") String exceKey, ModelMap mmap)
    {
        ExecAlgorithm execAlgorithm = execAlgorithmService.selectExecAlgorithmById(exceKey);
        mmap.put("execAlgorithm", execAlgorithm);
        List<Ification> ifications = ificationService.selectIficationPage(new Ification());
        mmap.put("ification",ifications);
        Algorithm algorithm = new Algorithm();
        algorithm.setIficationKey(execAlgorithm.getIficationKey());
        List<Algorithm> algorithms = algorithmService.selectAlgorithmList(algorithm);
        mmap.put("algorithms",algorithms);
        return prefix + "/detail";
    }


    /**
     * 提交shell文件
     */

    @RequestMapping("/uploadShell")
    @ResponseBody()
    public Map<String, Object> uploadShell(String exceKey) throws Exception{
        ExecAlgorithm execAlgorithm = execAlgorithmService.selectExecAlgorithmById(exceKey);

        String algorithmPara = execAlgorithm.getAlgorithmPara() + " " + shellService.getShellPath() + execAlgorithm.getAlgorithmFile();
        String algoRoute = execAlgorithm.getAlgorithmRoute();
        String distDir = algoRoute.substring(0,algoRoute.lastIndexOf("/"));
        String distFileName = algoRoute.substring(algoRoute.lastIndexOf("/") + 1);

        Map<String, Object> map = new HashMap<>();

        String newAlParam = "#!/bin/bash\n" + algorithmPara;

        boolean isUploadSuccess = shellService.uploadShellFile(RuoYiConfig.getProfile() + "/tmp.sh", newAlParam, distDir, distFileName);

        int result = isUploadSuccess ? 1 : 0 ;

        map.put("result", result);

        return  map;
    }

    /**
     * 修改保存算法执行  给完成标志赋值
     */

    @RequestMapping("/algorSubmit")
    @ResponseBody
    public AjaxResult algorSubmit(String exceKey)
    {
        int result =0;
        ExecAlgorithm execAlgorithm = execAlgorithmService.selectExecAlgorithmById(exceKey);
        try {
        //    Boolean flag =shellService.executeShell(execAlgorithm.getAlgorithmRoute(),
        //            shellService.getResultPath() + execAlgorithm.getAlgorithmFile());
            Boolean flag =shellService.executeShell(execAlgorithm.getAlgorithmRoute());
            if(flag){
                execAlgorithmService.algorSubmit(exceKey);
                result = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return toAjax(result);
    }


}
